﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using System.Threading;

namespace LED_Tester
{
    public partial class frmE1 : Form
    {
        public frmE1()
        {
            InitializeComponent();
        }

        #region 电源变量

        private ClassSourceMeter classSourceMeter;
        private int sourceMode;
        private SerialPort sptSource;
        private bool sourceIsJoin;
        private double voltSet;
        private double currSet;
        private double voltCheck;
        private double currCheck;

        public ClassSourceMeter Class_sourceMeter
        {
            get { return classSourceMeter; }
            set { classSourceMeter = value; }
        }
        public int SourceMode
        {
            get { return sourceMode; }
            set { sourceMode = value; }
        }
        public SerialPort SptSource
        {
            get { return sptSource; }
            set { sptSource = value; }
        }
        public bool SourceIsJoin
        {
            get { return sourceIsJoin; }
            set { sourceIsJoin = value; }
        }
        public double VoltSet
        {
            get { return voltSet; }
            set { voltSet = value; }
        }
        public double CurrSet
        {
            get { return currSet; }
            set { currSet = value; }
        }

        #endregion

        #region 其它变量

        private int index;
        private int timeSpace;
        private bool isMeasure = false;
        private bool isE1 = true;
        private string[] information;

        private Thread threadMeasure;
        private ClassAppConfigure classAppConfigure;

        private delegate void dlgShowButton();
        private delegate void dlgDisplay();

        public int TimeSpace
        {
            get { return timeSpace; }
            set { timeSpace = value; }
        }
        public string[] Information
        {
            get { return information; }
            set { information = value; }
        }

        #endregion

        #region 功能函数

        private void enableButton()
        {
            tlClear.Enabled = true;
            tlSave.Enabled = true;
            tlSetting.Enabled = true;
            tlMeasure.Enabled = true;
        }

        private void unableButton()
        {
            tlClear.Enabled = false;
            tlSave.Enabled = false;
            tlSetting.Enabled = false;
            tlMeasure.Enabled = false;
        }

        private void output()
        {
            int[] dataCount = new int[dgvList.ColumnCount - 1];

            for (int i = 0; i < dataCount.Length; i++)
            {
                dataCount[i] = 1;
            }

            SaveFileDialog sfdOutput = new SaveFileDialog();

            sfdOutput.Filter = "files (*.xls)|*.xls";
            sfdOutput.FilterIndex = 0;
            sfdOutput.Title = "保存为Excel文件";
            sfdOutput.FileName = DateTime.Now.ToString("yyyyMMdd") + DateTime.Now.Hour.ToString("00") + DateTime.Now.Minute.ToString("00"); //默认保存文件名
            if (sfdOutput.ShowDialog() == DialogResult.OK)
            {
                ClassExportExcel classExportExcel = new ClassExportExcel();
                classExportExcel.StartThread(dgvList, information, dataCount, sfdOutput.FileName);
            }
        }

        private void measure()
        {
            if (isMeasure)
            {
                do
                {
                    classSourceMeter.Set(sourceMode, sptSource, voltSet, currSet);
                    classSourceMeter.LightOn(sourceMode, sptSource);
                    Thread.Sleep(300);
                    classSourceMeter.Measure(sourceMode, sptSource, ref voltCheck, ref currCheck);
                    classSourceMeter.LightOff(sourceMode, sptSource);
                    BeginInvoke(new dlgDisplay(display));
                    Thread.Sleep(timeSpace);
                }
                while (true);
            }
        }


        private void display()
        {
            chartVoltage.ChartGroups.ChartGroupsCollection[0].ChartData.SeriesList[0].X.Add(currCheck);
            chartVoltage.ChartGroups.ChartGroupsCollection[0].ChartData.SeriesList[0].Y.Add(voltCheck);

            chartCurrent.ChartGroups.ChartGroupsCollection[0].ChartData.SeriesList[0].X.Add(index * timeSpace);
            chartCurrent.ChartGroups.ChartGroupsCollection[0].ChartData.SeriesList[0].Y.Add(currCheck);

            DataGridViewRow row = new DataGridViewRow();
            row.CreateCells(dgvList);

            row.Cells[0].Value = index.ToString("#00");
            row.Cells[1].Value = voltCheck.ToString("#0.0000");
            row.Cells[2].Value = currCheck.ToString("#0.0000");
            row.Cells[3].Value = (index * timeSpace).ToString();

            dgvList.Rows.Add(row);

            index++;
        }

        private void clear()
        {
            index = 1;

            chartVoltage.ChartGroups.ChartGroupsCollection[0].ChartData.SeriesList[0].PointData.Clear();
            chartCurrent.ChartGroups.ChartGroupsCollection[0].ChartData.SeriesList[0].PointData.Clear();

            dgvList.Rows.Clear();
        }

        private void loadConfig()
        {
            classAppConfigure = new ClassAppConfigure(Application.StartupPath + "\\data.ini.xml");
            timeSpace = classAppConfigure.TimeSpace;
        }

        private void saveConfig()
        {
            classAppConfigure.TimeSpace = timeSpace;
            classAppConfigure.Save();
        }

        #endregion

        #region 按钮事件

        private void tlSave_Click(object sender, EventArgs e)
        {
            if (dgvList.Rows.Count == 0)
            {
                return;
            }
            else
            {
                output();
            }
        }

        private void tlClear_Click(object sender, EventArgs e)
        {
            clear();
        }

        private void tlSetting_Click(object sender, EventArgs e)
        {
            frmLMeasSetting frm_LMeasSetting = new frmLMeasSetting();

            frm_LMeasSetting.VoltSet = voltSet;
            frm_LMeasSetting.CurrMax = currSet;

            frm_LMeasSetting.TimeSpace = timeSpace;

            frm_LMeasSetting.IsE1 = isE1;

            frm_LMeasSetting.ShowDialog();

            if (frm_LMeasSetting.DialogResult == DialogResult.OK)
            {
                voltSet = frm_LMeasSetting.VoltSet;
                currSet = frm_LMeasSetting.CurrMax;

                timeSpace = frm_LMeasSetting.TimeSpace;
            }
        }

        private void tlMeasure_Click(object sender, EventArgs e)
        {
            if (!sourceIsJoin)
            {
                MessageBox.Show("电源未连接！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            clear();

            threadMeasure = new Thread(new ThreadStart(measure));
            isMeasure = true;
            unableButton();
            threadMeasure.Start();
        }

        private void tlStop_Click(object sender, EventArgs e)
        {
            if (isMeasure)
            {
                classSourceMeter.LightOff(sourceMode, sptSource);

                if (threadMeasure != null)
                {
                    if (threadMeasure.IsAlive)
                    {
                        threadMeasure.Abort();
                    }
                }

                BeginInvoke(new dlgShowButton(enableButton));

                isMeasure = false;

                MessageBox.Show("暂停测试！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                MessageBox.Show("未进行测量！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
        }

        #endregion

        #region 窗体事件

        private void frmE1_Load(object sender, EventArgs e)
        {
            loadConfig();

            if (!sourceIsJoin)
            {
                MessageBox.Show("电源未连接！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

            clear();
        }

        private void frmE1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (isMeasure)
            {
                MessageBox.Show("正在测试！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                if (sourceIsJoin)
                {
                    classSourceMeter.LightOff(sourceMode, sptSource);
                }

                if (threadMeasure != null)
                {
                    if (threadMeasure.IsAlive)
                    {
                        threadMeasure.Abort();
                    }
                }

                saveConfig();
            }
        }

        #endregion
    }
}
